package LeetCode.linkednode;

public class LC_203 {
    public static ListNode removeElements(ListNode head, int val) {
        if(head == null){
            return null;
        }
        ListNode pre = head;
        ListNode cur = head.next;
        while (cur != null){  // 先从第二个节点开始删除
            if(cur.val == val){  // 如果cur值是data
                pre.next = cur.next;
            } else {  // 如果cur值不是data
                pre = cur;
            }
            cur = cur.next;
        }
        if(head.val == val){  //然后判断第一个节点值是否为data
            head = head.next;
        }
        return head;
    }
}
